$(document).ready(function() {

    $("#play").on("click",function(){$(".g-play").fadeIn();})
    $("#pause").on("click",function(){$(".g-play").fadeOut();$("#video").get(0).pause()})
    var NumberScropp = true;
    /* 头部尾部 */
    $(document).on("mousewheel DOMMouseScroll", function (e) {
        var delta = (e.originalEvent.wheelDelta && (e.originalEvent.wheelDelta > 0 ? 1 : -1)) || (e.originalEvent.detail && (e.originalEvent.detail > 0 ? -1 : 1));
        var WindowTop = $(window).scrollTop()+$(window).height();
        var BoxHeight = $("body").height();
        if (WindowTop-BoxHeight === 0 || $(window).scrollTop() === 0) {
            $(".g-foot").slideUp();
        }else{$(".g-foot").slideDown();};
        if (delta > 0) {
            $(".g-head").slideDown();
        } else if (delta < 0) {
            $(".g-head").slideUp();
        }
    });
    $(".g-foot>span").on("click",function(){
        $("html").animate({"scrollTop":'0'});
        $(".g-head").slideDown();
    })
    $(window).scroll(function(){
        var WindowTop = $(window).scrollTop()+$(window).height();
        if ($(".m-att").length === 0) {return false;};
        if (WindowTop>$(".m-att").offset().top && NumberScropp) {
            $("#num1").numberRock({speed:50,count:36})
            $("#num2").numberRock({speed:50,count:10000})
            $("#num3").numberRock({speed:50,count:100})
            $("#num4").numberRock({speed:50,count:30})
            NumberScropp = false;
         }
        if (WindowTop>$(".m-why>ul").offset().top) {
            anim(".m-why>ul>li","fadeInUp");
        };
        if (WindowTop>$(".m-test>div").offset().top) {
            anim(".m-test>div>div:first","fadeInLeft")
            anim(".m-test>div>div:first+div","fadeInRight")
        };
        if (WindowTop>$(".m-plan").offset().top) {
            anim(".m-plan>ul>li","fadeInUp")
        };
        if (WindowTop>$(".m-commitment>ul").offset().top) {
            anim(".m-commitment>ul>li","fadeInUp")
        };
        if (WindowTop>$(".m-guild>ul").offset().top) {
            anim(".m-guild>ul>li","fadeInUp")
        };

    })

    /* 免费测评 */
    $(window).scroll(function(){
        if ($(".m-plan3").length === 0) {return false;};
        var WindowTop = $(window).scrollTop()+$(window).height();

        if (WindowTop>$(".m-plan3").offset().top) {
            anim(".m-plan>ul>li","fadeInUp")
        };
    })
    /* 关于我们 */
    $(".m-about-left>li").each(function(i){$(this).on("click",AboutNav)})
    /*视频*/
    $(".about-video>span").each(function(i){$(this).on("click",VideoPlay)})
    $(".join-tab>li").each(function(i){$(this).on("click",JoinUs)})
});

/* 主页动画 */
function anim(dom,way){
    if($(dom).length>1) {
        $(dom).each(function(i){
            $(this).css({'animation': way+' 1s forwards '+$(this).index()*.5+"s"})
        })
    }else{$(dom).css({'animation': way+' 1s forwards'})};
}
/* about选项卡 */
function AboutNav(){
    $(this).addClass("on").siblings().removeClass("on");
    $(".m-about-right>li").eq($(this).index()).fadeIn("slow").siblings().fadeOut("slow");
}
/* 插入视频名字 */
var VideoArr = ['BG素材','BG素材']
/* 视频播放 */
function VideoPlay(){
    $(".g-play").fadeIn();
    $("#video").attr('src','video/'+VideoArr[$(this).index()]+".m4v")
}
/* 加入我们选项卡 */
function JoinUs() {
    $(this).addClass("on").siblings().removeClass("on");
    switch($(this).index())
    {
    case 0:
        $(".join>.join-us").fadeOut("slow").fadeIn("slow");
        break;
    case 1:
        $(".join-us").fadeOut("slow").siblings(".join-teacher").fadeIn("slow");
        break;
    case 2:
        $(".join-us").fadeOut("slow").siblings(".join-technicist").fadeIn("slow");
        break;
    case 3:
        $(".join-us").fadeOut("slow").siblings(".join-stylist").fadeIn("slow");
        break;
    default:
        $(".join-us").fadeOut("slow").siblings(".join-copywrite").fadeIn("slow");
    }
}
/* 轮播图 */
function Banner(){
    var index = 0;
    var Ban = $(".m-banner");
    var BanAll = $(".m-banner>div");
    var First = $(".m-banner>div").first();
    var Btn = $(".banner-btn");
    var Time = setInterval(Next,4000);
    Btn.css({"width":BanAll.length*90+"px"})
    Ban.on("mousemove",function(){clearInterval(Time)})
    Ban.on("mouseout",function(){clearInterval(Time);Time = setInterval(Next,4000);})
    function CreateDiv(){
        for(var i=0;i<BanAll.length;i++){
            Btn.append("<div></div>")
        }
        Btn.find("div").first().addClass('on')
        Ban.append(First.clone());
    }
    CreateDiv()
    $(".banner-btn").on("click","div",Nav)
    function Nav(){
        clearInterval(Time);
        index = $(this).index();
        $(this).addClass('on').siblings().removeClass("on");
        Ban.animate({"left":-index*BanAll.width()+"px"})
        Time = setInterval(Next,4000);
    }
    function Next(){
        if (index < BanAll.length-1) {
            index++;
            Ban.animate({"left":-index*BanAll.width()},1000)
        }else{
            index = 0;
            Ban.animate({"left":-BanAll.length*BanAll.width()},1000,function(){
                Ban.css({"left":0})
            })
        };
        $(".banner-btn").find("div").eq(index).addClass('on').siblings().removeClass("on");
    }
}
Banner()
